const { Model, DataTypes } = require("sequelize");
const sequelize = require("../app/database");
const User = require("../model/user.model");
const Apply = require("../model/apply.model");
const Config = require("../model/config.model");

class Question extends Model {}

Question.init(
  {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    materials_id: {
      type: DataTypes.INTEGER,
      references: {
        model: Apply,
        key: "id",
      },
    },
    title: {
      type: DataTypes.STRING,
    },
    header: {
      type: DataTypes.STRING,
    },
    question_time: {
      type: DataTypes.DATE,
    },
    round: {
      type: DataTypes.INTEGER,
    },
    config_id: {
      type: DataTypes.INTEGER,
      references: {
        model: Config,
        key: "id",
      },
    },
    createAt: {
      type: DataTypes.DATE,
    },
    updateAt: {
      type: DataTypes.DATE,
    },
  },
  {
    tableName: "question",
    createdAt: "createAt",
    updatedAt: "updateAt",
    timestamps: true,
    sequelize,
  }
);

// 将两张表联系在一起
Question.belongsTo(Apply, {
  foreignKey: "materials_id",
});

// 一对多
Question.belongsTo(Config, {
  foreignKey: "config_id",
});
Config.hasMany(Question, {
  foreignKey: "config_id",
});

module.exports = Question;
